import copy
import heapq
import math
from collections import defaultdict

n = int(input())
l = list(map(int, input().split()))
c = list(map(int, input().split()))
dp = defaultdict(lambda: float('inf'))
for i in range(n):
    dp[l[i]] = c[i]
heap = [(c[0], l[0])]


def dij():
    while heap:
        current_cost, current = heapq.heappop(heap)
        if current_cost > dp[current]: continue
        for next_el in copy.copy(list(dp.keys())):
            n_elem = math.gcd(next_el, current)
            if dp[n_elem] <= current_cost + dp[next_el]: continue
            dp[n_elem] = current_cost + dp[next_el]
            heapq.heappush(heap, (dp[n_elem], n_elem))
    if dp[1] == float('inf'):
        return -1
    return dp[1]


print(dij())
